Serialization and Deserialization
(Tap the post to see more)
Why?
• When we create an object it will eventually get destroyed when the process is finished. Instead of creating a new object to the same class, to use/access its data’s we can use the concept called serialization.
• The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform. To make a Java object serializable we implement the java.io.Serializable interface and also its subinterface java.io.Externalizable interface.
• This entire process in not dependent on the Java files. It can serialized in one end and deserialized at another end.
• Classes like ObjectInputStream and ObjectOutputStream are high level classes that are well versed in serializing and deserializing an object.
Serialization and Deserialization:
Serialization is a mechanism of
converting the state of an object into a byte stream. Deserialization is the
reverse process where the byte stream is used to recreate the actual Java
object in memory. This mechanism is used to persist the object.
package com.java.Serializing_deserializing;
import java.io.*; //importing the package
public class Serialization_class {
public static void main(String[] args) {
Employee emp = new Employee(); //creating an object for the emp class
emp.name = "Kishore ragav"; //assigning values using the instance created
emp.address = "Chengalpet";
emp.age = 21;
emp.salary = 22000;
try {
FileOutputStream fout = new FileOutputStream("F:\\Serialization.txt"); //creating a file
ObjectOutputStream objout = new ObjectOutputStream(fout); //converting the instance into stream of bytes
objout.writeObject(emp);
objout.close();
fout.close();
System.out.println("class has been serialized successfully");
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.java.Serializing_deserializing;
/* as we have serialized a method in order retrieve it to its object form we need to deserialize it*/
import java.io.*; //importing the package
public class Deserializing_class {
public static void main(String[] args) throws IOException, ClassNotFoundException { //throwing the exception that may be caused
Employee emp = null;
try {
FileInputStream filein = new FileInputStream("F:\\Serialization.txt");
ObjectInputStream objin = new ObjectInputStream(filein);
emp = (Employee) objin.readObject();
objin.close();
filein.close();
}
finally {
System.out.println("Deserializing the class");
System.out.println("The name of the Employee is " +emp.name);
System.out.println("The age of the employee is "+emp.age);
System.out.println("The address of the employee is "+emp.address);
System.out.println("The salary of the employee is "+emp.salary);
}
}
}
package com.java.Serializing_deserializing;
//Java program to illustrate customized serialization
import java.io.*;
class Custom implements Serializable {
String username = "lw_java";
transient String pwd = "java";
// Performing customized serialization using the below two methods:
// this method is executed by jvm when writeObject() on
// Account object reference in main method is
// executed by jvm.
private void writeObject(ObjectOutputStream oos) throws Exception
{
// to perform default serialization of Account object.
oos.defaultWriteObject();
// epwd (encrypted password)
String epwd = "123" + pwd;
// writing encrypted password to the file
oos.writeObject(epwd);
}
// this method is executed by jvm when readObject() on
// Account object reference in main method is executed by jvm.
private void readObject(ObjectInputStream ois) throws Exception
{
// performing default deserialization of Account object
ois.defaultReadObject();
// deserializing the encrypted password from the file
String epwd = (String)ois.readObject();
// decrypting it and saving it to the original password
// string starting from 3rd index till the last index
pwd = epwd.substring(3);
}
}
class ex1{
public static void main(String[] args) throws Exception
{
Custom custom = new Custom();
System.out.println("Username :" + custom.username +
" Password :" + custom.pwd);
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// writeObject() method on Account class will
// be automatically called by jvm
oos.writeObject(custom);
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Custom read_obj = (Custom)ois.readObject();
System.out.println("Username :" + read_obj.username +
" Password :" + read_obj.pwd);
}
}


Comments